<?php

namespace app\admin\service;

use app\admin\model\Abnormal;
use app\admin\model\City;
use app\admin\model\Disease;
use app\admin\model\Emergency;
use app\admin\model\Unit;
use app\admin\model\User;

/**
 * 道路交通-病害管理
 */
class EmergencyService extends BaseService
{
    public function __construct()
    {
        $this->name = 'YG';
        $this->model_type = 'emergency';
        $this->model = new Emergency();
    }

    public function lists(array $param)
    {
        $query = $this->model->where('mark', 1)->with(
            [
                'unit'     => function ($query) {
                    $query->field('id, unit_name');
                },
                'abnormal' => function ($query) {
                    $query->field('id, name');
                },
                'user'     => function ($query) {
                    $query->field('id, realname');
                }
            ]
        );
        if (!empty($param['operation_no'])) {
            $query->whereLike('operation_no', '%'.$param['operation_no'] . '%');
        }
        if (!empty($param['organization_id'])) {
            $query->where('organization_id', $param['organization_id']);
        }
        if (!empty($param['operation_category'])) {
            $query->where('operation_category', $param['operation_category']);
        }
        if (!empty($param['exception_category'])) {
            $query->where('exception_category', $param['exception_category']);
        }
        if (!empty($param['area_id'])) {
            $query->where('area_id', end($param['area_id']));
        }
        if (isset($param['organization_show'])) {
            $query->where('organization_show', $param['organization_show']);
        }
        if (isset($param['penalty'])) {
            $query->where('penalty', $param['penalty']);
        }
        if (isset($param['create_user'])) {
            $userid = User::whereLike('realname',"%".$param['create_user']."%")->column('id');
            if(empty($userid)){
                $userid=[0];
            }
            $query->whereIn('create_user', implode(',',$userid));
        }
        $data = [
            'msg'   => '操作成功',
            'code'  => 0,
            'data'  => [],
            'count' => (clone $query)->count('id'),
        ];
        if ($data['count']) {
            $data['data'] = $query->order('id', 'desc')
                ->page(PAGE, PERPAGE)
                ->select();

            $areaIds = [];
            foreach ($data['data'] as $val) {
                $areaIds[] = substr($val['area_id'], 0, 2) . '0000';
                $areaIds[] = substr($val['area_id'], 0, 4) . '00';
                $areaIds[] = $val['area_id'];
            }
            $areas = City::whereIn('citycode', $areaIds)->column('name', 'citycode');
            $unitModel = new Unit();
            $abmodel = new Abnormal();
            $adminModel = new User();
            foreach ($data['data'] as &$val) {
                $val['area'] = $areas[substr($val['area_id'], 0, 2) . '0000'] ?? '';
                $val['area'] .= $areas[substr($val['area_id'], 0, 4) . '00'] ?? '';
                $val['area'] .= $areas[$val['area_id']] ?? '';
                $val['area'] .= $val['address'];
                $unit = $unitModel->where(['id'=>$val['organization_id']])->find();
                $val['unit_name'] =$unit['unit_name'];
                $pval  = $abmodel->getInfo($val['exception_category']);
                $val['exception_name'] =empty($pval) ? '' : $pval['name'];
                $val['username'] = $adminModel->where(['id'=>$val['create_user']])->value('realname');
            }
        }

        return $data;
    }

    public function info()
    {
        $id              = request()->param('id', 0);
        $info            = $this->model->getInfo($id);
        $info['images']  = $info['images'] ? json_decode($info['images'], true) : [];
        $info['area_id'] = [
            (substr($info['area_id'], 0, 2) . '0000'),
            (substr($info['area_id'], 0, 4) . '00'),
            (string)$info['area_id']
        ];

        return message("操作成功", true, $info);
    }

    public function edit()
    {
        $data = request()->param();
        $data['area_id'] = end($data['area_id']);
        $data['images']  = json_encode($data['images']);

        return parent::edit($data);
    }

    public function setShow($id)
    {
        if (is_array($id)) {
            $query = Disease::where('id', 'in', $id);
        } else {
            $query = Disease::where('id', $id);
        }
        $query->update(['organization_show' => 1]);

        return message();
    }

}

